<?php

// 根据UID(usercode)获得游戏结果
require_once 'common/common.php';
require_once 'common/conn.php';

//requestURL($_SERVER['REQUEST_URI']);
$data = array();
if (isset($_GET['usercode'])) {
    $usercode = $_GET['usercode'];
    $db = new DBC();
    $sql = "SELECT deviceid,gold FROM t_device WHERE devicecode = '" . $usercode . "'";
    $device = $db->fetchData($sql);
    if ($device != false && count($device) > 0) {
        $deviceid = $device[0]['deviceid'];
        $data['gold'] = $device[0]['gold'];
        $table = "t_game_record_" . $deviceid;
        $sql = "SHOW TABLES LIKE '" . $table . "'";
        $tables = $db->fetchData($sql);
        if ($tables != false && count($tables) > 0) {
            // 1 胜利 -1 失败 0 平局
            $sql = "SELECT COUNT(id) AS number FROM " . $table . " WHERE result = 1";
            $win = $db->fetchData($sql);
            if ($win != false && count($win) > 0) {
                $data['win'] = $win[0]['number'];
            }
            if (!isset($data['win'])) {
                $data['win'] = "0";
            }
            $sql = "SELECT COUNT(id) AS number FROM " . $table . " WHERE result = 0";
            $draw = $db->fetchData($sql);
            if ($draw != false && count($draw) > 0) {
                $data['draw'] = $draw[0]['number'];
            }
            if (!isset($data['draw'])) {
                $data['draw'] = "0";
            }
            $sql = "SELECT COUNT(id) AS number FROM " . $table . " WHERE result = -1";
            $lost = $db->fetchData($sql);
            if ($lost != false && count($lost) > 0) {
                $data['lost'] = $lost[0]['number'];
            }
            if (!isset($data['lost'])) {
                $data['lost'] = "0";
            }
        } else {
            $sql = "CREATE TABLE `" . $table . "` (
            `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
            `usercode` varchar(50) DEFAULT NULL COMMENT 'uid,自动生成',
            `opponent` varchar(50) DEFAULT NULL COMMENT '对手',
            `gold` int(11) DEFAULT '0' COMMENT '获得金币数量',
            `gametime` datetime DEFAULT NULL COMMENT '游戏时间',
            `duration` int(11) DEFAULT '0' COMMENT '持续时间',
            `result` int(11) DEFAULT NULL COMMENT '1 胜利 -1 失败 0 平局',
            `right` int(11) DEFAULT '0' COMMENT '正确数',
            `wrong` int(11) DEFAULT '0' COMMENT '错误数',
            PRIMARY KEY (`id`)
            ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
            $db->execute($sql);
            $data['win'] = "0";
            $data['draw'] = "0";
            $data['lost'] = "0";
        }
        $sql = "SELECT nickname,sex,headID FROM t_device WHERE devicecode = '" . $usercode . "'";
        $temp = $db->fetchData($sql);
        if ($temp != false && count($temp) > 0) {
            if (isset($temp[0]['nickname'])) {
                $data['nickname'] = $temp[0]['nickname'];
            } else {
                $data['nickname'] = "unnamed";
            }

            $data['sex'] = $temp[0]['sex'];
            if (isNull($temp[0]['headID'])) {
                $data['headID'] = "BBAA000";
            } else {
                $data['headID'] = $temp[0]['headID'];
            }
        } else {
            $data['nickname'] = "unnamed";
            $data['sex'] = "-1";
        }
    } else {
        $data["errorNo"] = "1";
        $data["errorMsg"] = "UID:" . $usercode . " 不存在.";
    }
}
echo json_encode($data);
?>